package cn.labbook.bluetooth.util.p645;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2018/12/6.
 */
public class Protocol645Utils {

    public static byte[] toBytes(Protocol645 protocol) {

        ByteBuf byteBuf = Unpooled.buffer();

        byteBuf.writeByte(protocol.getOrigin1());
        byteBuf.writeBytes(protocol.getAddress());
        byteBuf.writeByte(protocol.getOrigin2());
        byteBuf.writeByte(protocol.getControl());
        byteBuf.writeByte(protocol.getL());
        byteBuf.writeBytes(protocol.getData());
        byteBuf.writeByte(calcCheckSum(protocol));
        byteBuf.writeByte(protocol.getTail());

        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);

        return bytes;
    }

    public static byte reduce(byte source, int reduce) {
        return (byte) (source - (byte) reduce);
    }

    /**
     * 获得完整的协议包的长度
     *
     * @param protocol
     * @return
     */
    public static int getCompleteProtocolLength(Protocol645 protocol) {
        return protocol.getL() + 12;
    }

    /***
     * 获取
     * @return
     */
    public static byte calcCheckSum(Protocol645 protocol) {
        ByteAddHandler byteAddHandler = new ByteAddHandler();
        byteAddHandler.setM(true);
        byteAddHandler.add(protocol.getOrigin1());
        byteAddHandler.add(protocol.getAddress());
        byteAddHandler.add(protocol.getOrigin2());
        byteAddHandler.add(protocol.getControl());
        byteAddHandler.add(protocol.getL());
        byteAddHandler.add(protocol.getData());
        return byteAddHandler.getSum();
    }


    public static Date p645timeTran(byte[] bytes) {
        if (bytes != null && bytes.length == 7) {
            byte sec = bytes[0];
            byte min = bytes[1];
            byte hour = bytes[2];
            byte weekDay = bytes[3];
            byte day = bytes[4];
            byte month = bytes[5];
            byte year = bytes[6];

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.SECOND, sub33AndTranHex(sec));
            calendar.set(Calendar.MINUTE, sub33AndTranHex(min));
            calendar.set(Calendar.HOUR_OF_DAY, sub33AndTranHex(hour));
            calendar.set(Calendar.DAY_OF_MONTH, sub33AndTranHex(day));
            calendar.set(Calendar.MONTH, sub33AndTranHex(month) - 1);
            calendar.set(Calendar.YEAR, 2000 + sub33AndTranHex(year));

            return calendar.getTime();
        } else {
            return null;
        }
    }

    public static byte[] set645Time(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second, Integer dayOfWeek) {
        ByteBuf byteBuf = Unpooled.buffer();
        if (second != null)
            byteBuf.writeByte(Protocol645Utils.tranHexAndAdd33ToByte(second));
        if (minute != null)
            byteBuf.writeByte(Protocol645Utils.tranHexAndAdd33ToByte(minute));
        if (hour != null)
            byteBuf.writeByte(Protocol645Utils.tranHexAndAdd33ToByte(hour));
        if (dayOfWeek != null)
            byteBuf.writeByte(Protocol645Utils.tranHexAndAdd33ToByte(dayOfWeek));
        if (day != null)
            byteBuf.writeByte(Protocol645Utils.tranHexAndAdd33ToByte(day));
        if (month != null)
            byteBuf.writeByte(Protocol645Utils.tranHexAndAdd33ToByte(month));
        if (year != null)
            byteBuf.writeByte(Protocol645Utils.tranHexAndAdd33ToByte(year));

        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);
        return bytes;
    }

    public static byte tranHexAndAdd33ToByte(int i) {
        return (byte) (Integer.parseInt(String.valueOf(i), 16) + 0x33);
    }

    public static int sub33AndTranHex(byte i) {
        return Integer.parseInt(Integer.toHexString((i & 0xff) - 0x33));
    }

}
